﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Drawing;
using System.IO;

namespace ConfExpert.Forms.SatinAlma
{
    public partial class _ImportList : XForm
    {
        public _ImportList()
        {
            InitializeComponent();
        }

        Color[] colors;
        private void ImportList_Load(object sender, EventArgs e)
        {
            

            gridView1.Init("GetAlisFaturasi2");

            colors = new Color[]{
            Color.WhiteSmoke,
            Color.Khaki,
            Color.FromArgb(238, 243, 213),

            Color.Khaki,
            Color.FromArgb(220, 232, 162)
            };

            PrepareBands(gridView1);
            //gridView1.DataTable().Columns["InvoiceTutar"].Expression = "(((((((((isnull([LineTotals],(0))+isnull([Deposit],(0)))+isnull([Bonus2Kalite],(0)))+isnull([Indirim],(0)))+isnull([_2KaliteIndirim],(0)))+isnull([NavlunBedeli],(0)))+isnull([KalipBedeli],(0)))+isnull([NumuneFarki],(0)))+isnull([Diger],(0)))+isnull([Surcharge],(0)))";
            gridView1.SetCaptions("SipNo:PO Nr.");
            gridView1.YanYanaGetir("SipNo:AlisFaturasiId");

            RepositoryItemComboBox riOdemesi = new RepositoryItemComboBox();
            riOdemesi.Items.AddRange(new object[] { "BEDELLİ", "BEDELSİZ" });
            riOdemesi.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            gridView1.Columns["Odemesi"].ColumnEdit = riOdemesi;

            RepositoryItemComboBox riBirimi = new RepositoryItemComboBox();
            riBirimi.Items.AddRange(new object[] { "MT", "ADET" });
            riBirimi.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            gridView1.Columns["Birim2"].ColumnEdit = riBirimi;

            gridView1.FormatAsNumber(5, "Oran");
            gridView1.MakeColumnsReadOnly("Ay", "LineTotals", "Miktar", "AlisFaturasiId");

            gridView1.FormatAsDate("InvoiceTarihi", "BeyannameTarihi", "KarbelInvTarihi");
            gridView1.FormatAsNumber(1, /*"InvoiceTutar",*/ "LineTotals", "Miktar", "İstatistikiKıymet", "KDV", "DamgaVergisi", "GumrukVergisi", "KKDF", "GumrukMusavirlikUcreti", "İtkibKayitBelgesi", "DahildeIsleme", "MesaiGVezAl", "OrdinoBedeli", "ArdiyeBedeli", "Demuraj", "NavlunUcreti", "SigortaUcreti", "İcNakliyeHizBed", "CezaGVezAl", "ITKIBKayit", "DigerMasraflar", "ToplamMasraf", "İthalatMasrafi");
            gridView1.AddSummary(/*"InvoiceTutar",*/ "LineTotals", "Miktar", "İstatistikiKıymet", "KDV", "DamgaVergisi", "GumrukVergisi", "KKDF", "GumrukMusavirlikUcreti", "İtkibKayitBelgesi", "DahildeIsleme", "MesaiGVezAl", "OrdinoBedeli", "ArdiyeBedeli", "Demuraj", "NavlunUcreti", "SigortaUcreti", "İcNakliyeHizBed", "CezaGVezAl", "ITKIBKayit", "DigerMasraflar", "ToplamMasraf", "İthalatMasrafi");

            RestoreLayout();
            bbiSaveLayout.ItemClick += bbiSaveLayout_ItemClick;

            gridView1.RowCellStyle += gridView1_RowCellStyle;
            beiView.EditValueChanged += AlisFaturasi_SelectedIndexChanged;
            beiView.EditValue = ytk(162) ? "KD" : (ytk(163) ? "İTD" : "Hepsi");
            //((RepositoryItemComboBox)beiView.Edit).SelectedIndexChanged += AlisFaturasi_SelectedIndexChanged;

            gridView1.BestFitColumns();
        }



        void AlisFaturasi_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (beiView.EditValue.ToString())
            {
                case "KD":
                    gridView1.Bands["gb1"].Visible = true;
                    gridView1.Bands["gb2"].Visible = false;
                    break;
                case "İTD":
                    gridView1.Bands["gb1"].Visible = false;
                    gridView1.Bands["gb2"].Visible = true;
                    break;
                case "Hepsi":
                    gridView1.Bands["gb1"].Visible =
                    gridView1.Bands["gb2"].Visible = true;
                    break;
            }
        }

        void bbiSaveLayout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gridControl1.MainView.SaveLayoutToXml(FileToSave/*, OptionsLayoutBase.FullLayout*/);
        }

        public string FileToSave
        {
            get
            {
                return Session.TempDirectory + "\\AF_LAYOUT2.xml";    //_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".xml";
            }
        }

        void RestoreLayout()
        {
            string layout = FileManager.ReadText(FileToSave);
            if (layout.Trim() == string.Empty)
                return;

            if (layout.Substring(0, 1) == "?")
                layout = layout.Substring(1);

            try
            {
                gridControl1.ForceInitialize();
                gridControl1.MainView.RestoreLayoutFromXml(FileToSave);
            }
            catch
            {
                File.Delete(FileToSave);
            }
        }


        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            GridView view = sender as BandedGridView;

            //if (e.RowHandle == view.FocusedRowHandle)
            //    e.Appearance.BackColor = Color.YellowGreen; // SystemColors.ControlDark;
            //else
            //    e.Appearance.Combine((e.RowHandle & 1) != 0 ? view.Appearance.EvenRow : view.Appearance.OddRow);

            if (e.RowHandle > -1)
                try
                {
                    if (e.Column.FieldName == "BrandId" ||
                        e.Column.FieldName == "FaturaNo")
                    {
                        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                    }

                    //if (e.Column.FieldName == "BolunecekMt")
                    //    e.Appearance.BackColor = Color.Khaki;

                    else if (e.RowHandle % 2 == 0 /*&& e.Appearance.BackColor == Color.White*/)
                        if (((BandedGridColumn)e.Column).OwnerBand != null)
                        {
                            int index = ((BandedGridColumn)e.Column).OwnerBand.Index;
                            e.Appearance.BackColor = colors[index];
                        }
                }
                catch
                {
                }

        }

        void PrepareBands(BandedGridView gv)
        {
            int cnt = 0;
            gv.Images = bandImages;
            string[] parts = "REFERANS;KUMAŞ BÖLÜMÜ;İTHALAT BÖLÜMÜ".Split(';');
            string[] starts = "AlisFaturasiId;LineTotals;KarbelReferans;".Split(';');
            for (int i = 0; i < parts.Length; i++)
            {
                GridBand gb = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
                gb.Caption = "<b>" + parts[i] + "</b>";
                gb.ImageIndex = i;
                gb.Name = "gb" + gb.ImageIndex.ToString();
                //TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gb.OptionsBand.AllowHotTrack = true;
                gv.Bands.Add(gb);

                for (int i2 = cnt; i2 < gv.Columns.Count; i2++)
                {
                    if (gv.Columns[cnt].FieldName == starts[i + 1] && cnt > 0)
                        break;

                    gv.Bands[i].Columns.Add(gv.Columns[cnt]);

                    cnt++;
                }
            }

            //RepositoryItemGridLookUpEdit ri = new RepositoryItemGridLookUpEdit();
            //ri.DataSource = new DataView(ExecDatatable("GetPrdTree"));
            //ri.DisplayMember = "Name";
            //ri.ValueMember = "PrdTreeId";
            //ri.NullText = string.Empty;

            //gv.Columns["PrdTreeId"].ColumnEdit = RI(Rp.PrdTree);

            gv.OptionsView.AllowHtmlDrawHeaders = true;
        }

    }
}
